<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.services.msgbroker &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../_static/alabaster.css" />
    <script data-url_root="../../../" id="documentation_options" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.services.msgbroker</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    Msg Broker Service. Every component of a process can produce_msg.</span>

<span class="sd">    This msg broker feature is still exprimental. Do not use it in production</span>
<span class="sd">    until this comment is deleted.</span>
<span class="sd">&quot;&quot;&quot;</span>


<span class="n">MSG_ERROR_DISK_ERROR</span> <span class="o">=</span> <span class="mi">1</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;BrokerCenter&#39;</span><span class="p">,</span> <span class="s1">&#39;SystemErrmsgBroker&#39;</span><span class="p">]</span>


<span class="n">MSG_TYPE_FATAL</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">MSG_TYPE_WARN</span> <span class="o">=</span> <span class="mi">1</span>


<span class="k">class</span> <span class="nc">BaseBroker</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Base Broker for a system</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">_name</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_name</span> <span class="o">=</span> <span class="n">name</span>


<div class="viewcode-block" id="BrokerCenter"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.BrokerCenter">[docs]</a><span class="k">class</span> <span class="nc">BrokerCenter</span><span class="p">(</span><span class="n">BaseBroker</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Errmsg broker center</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">BaseBroker</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

<div class="viewcode-block" id="BrokerCenter.produce_msg"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.BrokerCenter.produce_msg">[docs]</a>    <span class="k">def</span> <span class="nf">produce_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">,</span> <span class="n">extra_info</span><span class="p">,</span> <span class="n">error</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;register msg&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="BrokerCenter.comsume_msg"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.BrokerCenter.comsume_msg">[docs]</a>    <span class="k">def</span> <span class="nf">comsume_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get msg_type from the broker center</span>
<span class="sd">        &quot;&quot;&quot;</span></div></div>


<div class="viewcode-block" id="SystemErrmsgBroker"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker">[docs]</a><span class="k">class</span> <span class="nc">SystemErrmsgBroker</span><span class="p">(</span><span class="n">BrokerCenter</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    system errmsg broker, you can use it to determine whether</span>
<span class="sd">    exiting from the system is on the way</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="n">BrokerCenter</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

<div class="viewcode-block" id="SystemErrmsgBroker.need_stop"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.need_stop">[docs]</a>    <span class="k">def</span> <span class="nf">need_stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return True if the system registered on</span>
<span class="sd">            the path needs to stop immediately</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.fatal_alert"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.fatal_alert">[docs]</a>    <span class="k">def</span> <span class="nf">fatal_alert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">need_stop</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;fatal alert systems&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.warnning_alert"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.warnning_alert">[docs]</a>    <span class="k">def</span> <span class="nf">warnning_alert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        warnning alert</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.register_msg"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.register_msg">[docs]</a>    <span class="k">def</span> <span class="nf">register_msg</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msgtype</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;register msg into the system&quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.get_fatal_alerts"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.get_fatal_alerts">[docs]</a>    <span class="k">def</span> <span class="nf">get_fatal_alerts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        get fatal alerts of the current running round</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.clean_data"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.clean_data">[docs]</a>    <span class="k">def</span> <span class="nf">clean_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">exclude_msgtypes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        clean data of the remaining data</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="SystemErrmsgBroker.register_wakeup"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.register_wakeup">[docs]</a>    <span class="k">def</span> <span class="nf">register_wakeup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msgtype</span><span class="p">,</span> <span class="n">alert_cap_num</span><span class="p">,</span> <span class="n">callfunc</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register wakeups.</span>

<span class="sd">        :param alert_cap_num:</span>
<span class="sd">            If alert_cap_num is 0, whenever a msg of msgtype is received,</span>
<span class="sd">            the callfunc will be called.</span>
<span class="sd">        :param msgtype:</span>
<span class="sd">            [msgbroker.FATAL|msgbroker.WARN]</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

    <span class="k">def</span> <span class="nf">_wakeup</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msgtype</span><span class="p">,</span> <span class="n">alert_cap_num</span><span class="p">,</span> <span class="n">callfunc</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        wake up callfunc</span>
<span class="sd">        &quot;&quot;&quot;</span>

<div class="viewcode-block" id="SystemErrmsgBroker.register_msgtype_callback"><a class="viewcode-back" href="../../../cup.services.html#cup.services.msgbroker.SystemErrmsgBroker.register_msgtype_callback">[docs]</a>    <span class="k">def</span> <span class="nf">register_msgtype_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">msg_type</span><span class="p">,</span> <span class="n">callback_func</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register msgtype with callback functions</span>
<span class="sd">        &quot;&quot;&quot;</span></div></div>

<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>